#include "widget.h"

/**
 * @brief Widget::Widget
 *        初始化界面的布局，模仿的应用"有道词典"
 * @param parent
 */
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    main_box = new QHBoxLayout;
    m_main_splitter = new QSplitter(this);
    m_main_left = new main_left_widget;
    m_main_right = new main_right_widget(this);
    m_main_image = new QImage;
    this->setObjectName("Widget");
    m_main_splitter->setObjectName("m_main_splitter");

    /* 2 添加左右布局，左边显示功能栏，右边显示对应的内容 */
    m_main_splitter->setOrientation(Qt::Horizontal);

    m_main_splitter->addWidget(m_main_left);
    m_main_splitter->addWidget(m_main_right);

    m_main_splitter->setStretchFactor(0,2);     //设置比例
    m_main_splitter->setStretchFactor(1,8);

    m_main_splitter->setHandleWidth(0);         //隐藏分割线
    main_box->addWidget(m_main_splitter);
    main_box->setMargin(0);
    this->setLayout(main_box);

    /* 3 加载样式表文件 default.css */
    QFile fCss(":/qss/qss/default.css");
    if( !fCss.open(QFile::ReadOnly))
    {
        qDebug("css File load false");
        return;
    }
    QString strCssContent(fCss.readAll());
    setStyleSheet(strCssContent);
    fCss.close();

    /* 2.3 设置背景图片 */
    //m_main_image->load(":/img/img/main_background.jpg");
    //this->setAutoFillBackground(true);
    //QPalette pal(palette());
    //pal.setBrush(QPalette::Window, QBrush(m_main_image->scaled(size(), Qt::IgnoreAspectRatio,
    //                      Qt::SmoothTransformation)));
    //this->setPalette(pal);

    /* 2.4 设置图标,隐藏标题栏 */
    this->setWindowTitle("基于物联网的学习系统设计");
    QIcon icon(":/img/img/time.ico");
    this->setWindowIcon(icon);
    connect(m_main_splitter,SIGNAL(splitterMoved(int,int)),this,SLOT(on_m_main_splitter_Moved(int,int)));
    connect(m_main_left,SIGNAL(sigClockButtonClicked()),m_main_right,SLOT(showMyClock()));
    connect(m_main_left,SIGNAL(sigBtnStudyClicked()),m_main_right,SLOT(showStudyWidget()));
    connect(m_main_left,SIGNAL(sigBtnFriendClicked()),m_main_right,SLOT(showFriendWidget()));
    connect(m_main_left,SIGNAL(sigBtnNewsClicked()),m_main_right,SLOT(showDailyWidget()));
    connect(m_main_left,SIGNAL(sigSelectionChanged()),m_main_right->m_study_widget->m_bottom_widget,SLOT(updateSelectedDate()));
    connect(m_main_right->m_friend_widget->onepersonplot,SIGNAL(sigFriendChange()),
            m_main_right->m_friend_widget->onepersonplot,SLOT(dataUpdate()));
    connect(m_main_left,SIGNAL(sigFriendDataUpdate()),
            m_main_right->m_friend_widget->onepersonplot,SLOT(dataUpdate()));
}

/**
 * @brief Widget::~Widget
 */
Widget::~Widget()
{

}

void Widget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
/**
 * @brief Widget::setWidgetsSize
 *      设置各个窗口的大小
 */
void Widget::setWidgetsSize()
{
    QDesktopWidget* desktopWidget =QApplication::desktop();
    QRect deskRect =desktopWidget->availableGeometry();   //获取可用桌面大小
    /* 1 设置主界面的最小尺寸 */
    //this->resize(deskRect.width(),deskRect.height());
    this->setMinimumHeight(deskRect.height());
    this->setMinimumWidth(deskRect.width());
    m_main_left->setMaximumWidth(this->width()/4);
    m_main_right->setMinimumWidth(this->width()-m_main_left->width());

    /* 2.设置三个主要界面的最下尺寸 */
    m_main_right->m_study_widget->setMinimumHeight(m_main_right->height());
    m_main_right->m_study_widget->setMinimumWidth(m_main_right->width());
    m_main_right->m_daily_widget->setMinimumHeight(m_main_right->height());
    m_main_right->m_daily_widget->setMinimumWidth(m_main_right->width());
    m_main_right->m_friend_widget->setMinimumHeight(m_main_right->height());
    m_main_right->m_friend_widget->setMinimumWidth(m_main_right->width());

    /* 3.************** */
    m_main_right->m_study_widget->m_middle_widget->setMaximumHeight(m_main_right->m_study_widget->height()/2);
    m_main_right->m_study_widget->m_middle_widget->setMinimumHeight(m_main_right->m_study_widget->height()/2);

    m_main_right->m_study_widget->m_middle_widget->m_clock->setMaximumHeight(m_main_right->m_study_widget->height()/2);
    m_main_right->m_study_widget->m_middle_widget->m_clock->setMinimumHeight(m_main_right->m_study_widget->height()/2);
    m_main_right->m_study_widget->m_middle_widget->m_clock->setMaximumWidth(m_main_right->m_study_widget->width()/3);

    m_main_right->m_study_widget->m_bottom_widget->setMinimumHeight(m_main_right->m_study_widget->height()/4);

    /* 4.设置好友进度的相关窗体大小 */
    m_main_right->m_friend_widget->scroll->setMinimumWidth(m_main_right->width());
    m_main_right->m_friend_widget->scroll->setMinimumHeight(m_main_right->height());
    m_main_right->m_friend_widget->scrollAreaWidgetContents->setMinimumWidth(m_main_right->width());
    m_main_right->m_friend_widget->scrollAreaWidgetContents->setMinimumHeight(m_main_right->height()*2);

    m_main_right->m_friend_widget->dataplot->setMinimumWidth(m_main_right->width()*0.9);
    m_main_right->m_friend_widget->dataplot->setMinimumHeight(m_main_right->height()*0.84);
    m_main_right->m_friend_widget->onepersonplot->setMinimumWidth(m_main_right->width()*0.9);
    m_main_right->m_friend_widget->onepersonplot->setMinimumHeight(m_main_right->height()*0.6);
    m_main_right->m_friend_widget->dataplot->diagram->setMinimumWidth(m_main_right->width()*0.9);
    m_main_right->m_friend_widget->dataplot->diagram->setMinimumHeight(m_main_right->height()*0.84);

    m_main_right->m_friend_widget->onepersonplot->container->setMinimumWidth(m_main_right->width()*0.9);
    m_main_right->m_friend_widget->onepersonplot->container->setMinimumHeight(m_main_right->height()*0.6);
    m_main_right->m_friend_widget->onepersonplot->diagram->setMinimumWidth(m_main_right->width()*0.9);
    m_main_right->m_friend_widget->onepersonplot->diagram->setMinimumHeight(m_main_right->height()*0.6);

    /* 4.设置学习日报相关窗体大小 */
//    m_main_right->m_daily_widget->scroll->setMinimumWidth(m_main_right->width());
//    m_main_right->m_daily_widget->scroll->setMinimumHeight(m_main_right->height());
//    m_main_right->m_daily_widget->scrollAreaWidgetContents->setMinimumWidth(m_main_right->width());
//    m_main_right->m_daily_widget->scrollAreaWidgetContents->setMinimumHeight(m_main_right->height()*2);


    //m_main_right->m_daily_widget->plusBtn->move(this->width()-80,this->height()-80);
    //printWidgetSize();
}

void Widget::printWidgetSize()
{
    qDebug()<<"widget size";
    qDebug()<<"m_main_right->height():"<<m_main_right->height();
    qDebug()<<"m_main_right->width():"<<m_main_right->width();
    qDebug()<<"m_main_right->m_friend_widget->scrollAreaWidgetContents->height():"<<m_main_right->m_friend_widget->scrollAreaWidgetContents->height();
    qDebug()<<"m_main_right->m_friend_widget->scrollAreaWidgetContents->width():"<<m_main_right->m_friend_widget->scrollAreaWidgetContents->width();
    qDebug()<<"m_main_right->m_friend_widget->dataplot->diagram->width():"<<m_main_right->m_friend_widget->dataplot->diagram->width();
    qDebug()<<"m_main_right->m_friend_widget->dataplot->diagram->height():"<<m_main_right->m_friend_widget->dataplot->diagram->height();
}

void Widget::on_m_main_splitter_Moved(int pos, int index)
{
    this->setWidgetsSize();
}
